﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls.Input;
using Telerik.Windows.Data;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace WebPiket3Note
{
    /// <summary>
    /// Логика взаимодействия для NoteWindow.xaml
    /// </summary>
    public partial class NoteWindow : Window
    {
        /// <summary>
        /// Конструктор
        /// </summary>
        public NoteWindow()
        {
            InitializeComponent();
            pikets = new Sources.PiketsJournal();
            PrjProperty.Init();
            gridView1.ItemsSource = Sources.SourcesInterface.piketJL.Pikets;
        }

        Sources.PiketsJournal pikets;

        /// <summary>
        /// Кнопка новый
        /// </summary>
        private void RadRibbonButton_Click_1(object sender, RoutedEventArgs e)
        {
            PrjProperty.Init();

            //PrjProperty.Sources.SourcesInterface.piketJL.MyCave();//Создать тестовую пещеру

            gridView1.ItemsSource = Sources.SourcesInterface.piketJL.Pikets;
        }

        /// <summary>
        /// Кнопка Открыть
        /// </summary>
        private void RadRibbonButton_Click_2(object sender, RoutedEventArgs e)
        {
            PrjProperty.Init();
            PrjProperty.XmlLoadPiketsJournal("data.xml");
            gridView1.ItemsSource = Sources.SourcesInterface.piketJL.Pikets;
        }

        /// <summary>
        /// Импорт пикета
        /// </summary>
        private void RadRibbonButton_Click_Wizard(object sender, RoutedEventArgs e)
        {
            ImportPiket.ImportWizardWindow impWnd = new ImportPiket.ImportWizardWindow();
            impWnd.ShowDialog();
            Sources.Piket piket = new Sources.Piket();
            foreach (Sources.PiketString pkt in Sources.SourcesInterface.piketstr)
            {
                piket = new Sources.Piket();
                piket.IsPiket = pkt.IsPiket;
                piket.From = pkt.From;
                piket.To = pkt.To;
                piket.Desc = pkt.Desc;

                piket.Length = PrjProperty.ToNumeric(pkt.Length);
                piket.Bearing = PrjProperty.ToNumeric(pkt.Bearing);
                piket.Inc = PrjProperty.ToNumeric(pkt.Inc);

                piket.Left = PrjProperty.ToNumeric(pkt.Left);
                piket.Right = PrjProperty.ToNumeric(pkt.Right);
                piket.Up = PrjProperty.ToNumeric(pkt.Up);
                piket.Down = PrjProperty.ToNumeric(pkt.Down);

                Sources.SourcesInterface.piketJL.Pikets.Add(piket);
            }
            gridView1.Rebind();
        }
        /// <summary>
        /// Кнопка Сохранить
        /// </summary>
        private void RadRibbonButton_Click_3(object sender, RoutedEventArgs e)
        {
            PrjProperty.XmlSavePiketsJournal("data.xml");
        }

        /// <summary>
        /// Добавить выше
        /// </summary>
        private void RadRibbonButton_Click_AddBefore(object sender, RoutedEventArgs e)
        {
            // позиция курсора
            int noCur = Sources.SourcesInterface.piketJL.Pikets.IndexOf(gridView1.SelectedItem as Sources.Piket);

            // проверяет если выделена строка, то добавлят перед ней, иначе добавляет в начало
            if (Sources.SourcesInterface.piketJL.Pikets.Count == 0)
                Sources.SourcesInterface.piketJL.Pikets.Add(new Sources.Piket());
            else
                if (noCur == -1)
                    Sources.SourcesInterface.piketJL.Pikets.Insert(0, new Sources.Piket());
                else
                    Sources.SourcesInterface.piketJL.Pikets.Insert(noCur, new Sources.Piket());
            gridView1.Rebind();
        }

        /// <summary>
        /// Добавить ниже
        /// </summary>
        private void RadRibbonButton_Click_AddAfter(object sender, RoutedEventArgs e)
        {
            // позиция курсора
            int noCur = Sources.SourcesInterface.piketJL.Pikets.IndexOf(gridView1.SelectedItem as Sources.Piket);

            // проверяет если выделена строка, то добавлят после нее иначе добавляет в начало
            if (Sources.SourcesInterface.piketJL.Pikets.Count == 0)
                Sources.SourcesInterface.piketJL.Pikets.Add(new Sources.Piket());
            else
                if (noCur == -1)
                    Sources.SourcesInterface.piketJL.Pikets.Add(new Sources.Piket());
                else
                    if (noCur + 1 == Sources.SourcesInterface.piketJL.Pikets.Count)
                        Sources.SourcesInterface.piketJL.Pikets.Add(new Sources.Piket());
                    else
                        Sources.SourcesInterface.piketJL.Pikets.Insert(noCur + 1, new Sources.Piket());
            gridView1.Rebind();
        }

        /// <summary>
        /// Удалить строку
        /// </summary>
        private void RadRibbonButton_Click_Del(object sender, RoutedEventArgs e)
        {
            // позиция курсора
            int noCur = Sources.SourcesInterface.piketJL.Pikets.IndexOf(gridView1.SelectedItem as Sources.Piket);

            // Если выделена строка, удаляет
            if (noCur != -1)
                Sources.SourcesInterface.piketJL.Pikets.RemoveAt(noCur);
            gridView1.Rebind();
        }

        /// <summary>
        /// Расчитать поля приращений
        /// </summary>
        private void RadRibbonButton_Click_CalculateDxDyDz(object sender, RoutedEventArgs e)
        {
            // Пересчитать поля приращений для всех строк с флагом IsPiket
            PrjProperty.CalculateDxDyDz();

            gridView1.Rebind();
        }

    }
}
